package com.flw.eslab.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import com.flw.eslab.dao.UserDao;
import com.flw.eslab.dto.PageInfo_Dto;
import com.flw.eslab.dto.User_Dto;
import com.flw.eslab.model.User;
import com.flw.eslab.service.UserManager;


//@Service("userManager")
public class UserManagerImpl implements UserManager {
	private UserDao userDao;

	/*------------------------------------------*/


	@Override
	public User_Dto check(String studentNO, String password) {
		List<User> list = userDao.checkAndReturnUser(studentNO, password);
		if (!list.isEmpty())
		{
			User user=list.get(0);
			User_Dto user_Dto=new User_Dto();
			user_Dto.setId(user.getId());
			user_Dto.setName(user.getName());
			user_Dto.setPosition(user.getPosition());
			user_Dto.setState(user.getState());
			user_Dto.setStudentNO(user.getStudentNO());
			return user_Dto;
			
		}

		else
			return new User_Dto(-1);
	}

	@Override
	public User getUserByID(int id) {
		return (User) userDao.findById(User.class, id);
	}
	@Override
	public boolean checkStudentNo(String studentNO) {
		List<User> list = userDao.checkAndReturnUser(studentNO);
		System.out.println("list:" + list.isEmpty());
		if (list.isEmpty())
			return true;
		else
			return false;
	}

	/*------------------------------------------*/
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public boolean add(User user) {
		if(user.getStudentNO()==null||user.getStudentNO().equals(""))
			return false;
		userDao.save(user);
		return true;
	}

	@Override
	public boolean modify(User user) {
		if(user.getId()==null||user.getStudentNO()==null)
			return false;
		if(userDao.findById(User.class, user.getId())==null)
			return false;
		userDao.update(user);
		return true;
	}

	@Override
	public boolean delete(User user) {
		if(user.getId()==null)
			return false;
		if(userDao.findById(User.class, user.getId())==null)
			return false;
		userDao.deleteById(User.class, user.getId());
		return true;
	}

	@Override
	public PageInfo_Dto load(PageInfo_Dto pageInfo_Dto) {
		int pageNow=pageInfo_Dto.getPageNow()==null?1:pageInfo_Dto.getPageNow();
		int pageSize=pageInfo_Dto.getPagesize()==null?5:pageInfo_Dto.getPagesize();	
		return userDao.findByPageInfo(User.class, pageNow, pageSize, null, null);
	}

}
